/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hl7.datatypes.r2.foundation;

import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.hl7.datatypes.r2.flavors.st.ST_SIMPLE;

/**
 *
 * @author Obama
 */
@Embeddable
public class BL extends ANY implements Serializable {

    @Column(name = "BL_VALUE")
    private Boolean booleanValue;

    public BL(Boolean value) {
        this.booleanValue = value;
    }

    public BL() {
    }

    public BL not() {
        if (booleanValue == null) {
            return null;
        } else {
            return new BL(!booleanValue);
        }
    }

    public BL and(BL x) {
        if (booleanValue != null && x != null && x.getBooleanValue() != null) {
            return new BL(booleanValue & x.getBooleanValue());
        } else if (booleanValue != null && booleanValue.booleanValue() == false || (x != null && x.getBooleanValue() != null && x.getBooleanValue().booleanValue() == false)) {
            return new BL(false);
        } else {
            return null;
        }
    }

    public BL or(BL x) {
        if (booleanValue != null && x != null && x.getBooleanValue() != null) {
            return new BL(booleanValue | x.getBooleanValue());
        } else if (booleanValue != null && booleanValue.booleanValue() == true || (x != null && x.getBooleanValue() != null && x.getBooleanValue().booleanValue() == true)) {
            return new BL(true);
        } else {
            return null;
        }
    }

    public BL xor(BL x) {
        if (booleanValue != null && x != null && x.getBooleanValue() != null) {
            return or(x).and(and(x).not());
        } else {
            return null;
        }
    }

    public BL implies(BL x) {
        if (booleanValue != null && x != null && x.getBooleanValue() != null) {
            return not().or(x);
        } else {
            return null;
        }
    }

    public ST_SIMPLE literal() {
        return new ST_SIMPLE(toString());
    }

    @Override
    public String toString() {
        return String.valueOf(booleanValue);
    }

    public Boolean getBooleanValue() {
        return booleanValue;
    }

    public void setBooleanValue(Boolean booleanValue) {
        this.booleanValue = booleanValue;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 41 * hash + Objects.hashCode(this.booleanValue);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null||this.booleanValue==null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final BL other = (BL) obj;
        if (!Objects.equals(this.booleanValue, other.booleanValue)) {
            return false;
        }
        return true;
    }
}
